package cn.edu.hit.kg.data;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.conf.ConfigureLoader;

import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.StreamSupport;

/**
 * Created by Wang on 2018/11/19.
 */
public interface GraphDataProvider {
    Map<Long, Node> getNodes();

    Map<Long, Set<Long>> getEdges();

    Map<String, Set<Long>> getLabelGroups();

    boolean config(Map configure);

    static GraphDataProvider getInstance() {
        ConfigureLoader configureLoader = ConfigureLoader.getInstance();
        Document document = configureLoader.getBaseConfigure();

        return getGraphDataProvider(document);
    }

    static GraphDataProvider getGraphDataProvider(Document document) {
        ServiceLoader<GraphDataProvider> loader = ServiceLoader.load(GraphDataProvider.class);
        Iterator<GraphDataProvider> iterator = loader.iterator();
        while (iterator.hasNext()) {
            GraphDataProvider provider = iterator.next();
            if (provider.config(document)) {
                return provider;
            }
        }
        return null;
    }


}
